# Replication Archive for: Schwarz, Susanne and Alexander Coppock. (2020) 
# "What Have We Learned About Gender From Candidate Choice Experiments? 
# A Meta-analysis of 67 Factorial Survey Experiments" 
# Forthcoming, Journal of Politics.

rm(list = ls())
library(estimatr)
library(tidyverse)
library(coefplot)
library(rmeta)

source("helpers.R")

cand_attributes_cate_estimates <-
  read_rds("cand_attributes_cate_estimates.rds")


# Figure A.1 --------------------------------------------------------------


g <-
  ggplot(cand_attributes_cate_estimates,
         aes(x = estimate_100, y = unique_label_fac))  +
  geom_point(position = position_dodgev(height = 0.5), alpha = 0.5) +
  geom_errorbarh(
    aes(xmin = conf.low_100, xmax = conf.high_100),
    position = position_dodgev(height = 0.5),
    height = 0,
    alpha = 0.5
  ) +
  geom_vline(xintercept = 0, linetype = "dashed") +
  coord_cartesian(xlim = c(-20, 20)) +
  xlab("CATE estimate in percentage points") +
  theme_bw() +
  theme(
    axis.title.y = element_blank(),
    axis.text.y = element_blank(),
    axis.ticks.y = element_blank(),
    panel.grid.major.y = element_blank()
  )
g

# ggsave(
#   "cand_cates.pdf",
#   g,
#   width = 6.5,
#   height = 8
# )


# Figures A.2 through A. 45 -----------------------------------------------

g <-
  ggplot(NULL, aes(x = estimate_100, y = CATE_label))  +
  geom_point(position = position_dodgev(height = 0.5), alpha = 0.5) +
  geom_errorbarh(
    aes(xmin = conf.low_100, xmax = conf.high_100),
    position = position_dodgev(height = 0.5),
    height = 0,
    alpha = 0.5
  ) +
  geom_vline(xintercept = 0, linetype = "dashed") +
  coord_cartesian(xlim = c(-20, 20)) +
  xlab("CATE estimate in percentage points") +
  theme_bw() +
  theme(axis.title.y = element_blank())

studies <- unique(cand_attributes_cate_estimates$study_id)
for (study in studies) {
  df_local <- cand_attributes_cate_estimates %>% filter(study_id %in% study)
  g_local <- g %+% (df_local) +
    geom_text(
      x = 20,
      aes(y = CATE_label, label = se_entry),
      hjust = 1,
      size = 3,
      nudge_y = 0.2
    )
  
  # ggsave(
  #   paste0(
  #     "cand_figure_",
  #     study,
  #     ".pdf"
  #   ),
  #   g_local,
  #   width = 6.5,
  #   height = 1 + nrow(df_local) * 0.15
  # )
}

# Fixing MO
df_local <- 
  cand_attributes_cate_estimates %>% filter(study_id %in% "mo_1")
g_local <-
  g %+% (df_local) + geom_text(
    x = 80,
    aes(y = CATE_label, label = se_entry),
    hjust = 1,
    size = 3,
    nudge_y = 0.2
  ) +
  coord_cartesian(xlim = c(-80, 80))

# ggsave(
#   paste0(
#     "04_Drafts/current_draft/figures/cand_figure_",
#     "mo_1",
#     ".pdf"
#   ),
#   g_local,
#   width = 6.5,
#   height = 1 + nrow(df_local) * 0.15
# )

studies_long <- c("henderson_1", "bansak_1", "bansak_2")
for (study in studies_long) {
  df_local <- cand_attributes_cate_estimates %>% filter(study_id %in% study)
  g_local <-
    g %+% (df_local) + theme(axis.text.y = element_text(size = 4)) +
    geom_text(
      x = 20,
      aes(y = CATE_label, label = se_entry),
      hjust = 1,
      size = 1,
      nudge_y = 0.2
    )
  
  # ggsave(
  #   paste0(
  #     "04_Drafts/current_draft/figures/cand_figure_",
  #     study,
  #     ".pdf"
  #   ),
  #   g_local,
  #   width = 6.5,
  #   height = 2 + nrow(df_local) * 0.08
  # )
}



# Table A.1 ---------------------------------------------------------------

study_df <-
  cand_attributes_cate_estimates %>%
  group_by(study_id, study_name) %>%
  summarize(n = n())

# sink("cand_cates.tex")
# for (i in 1:nrow(study_df)) {
#   cat(
#     "\\begin{figure}\n",
#     "\\centering\n",
#     paste0("\\caption{", study_df$study_name[i], "}\n"),
#     paste0("\\label{fig:cate_", study_df$study_id[i], "}\n"),
#     paste0(
#       "\\includegraphics[width = \\textwidth]{figures/cand_figure_",
#       study_df$study_id[i],
#       ".pdf}\n"
#     ),
#     "\\end{figure}\n",
#     "\n\n"
#   )
# }
# sink()
